package com.zwps.common.core.config;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.zwps.common.api.model.vo.LoginUserVO;
import com.zwps.common.core.context.LoginInfoContext;
import com.zwps.common.core.service.LogService;
import com.zwps.extra.http.LogAPIMessage;
import com.zwps.extra.http.RequestLogRecord;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class CoreConfig {
    private static ObjectMapper objectMapper;

    public static synchronized ObjectMapper createObjectMapper() {
        if (objectMapper == null) {
            objectMapper = new ObjectMapper();
        } else {
            return objectMapper;
        }

        // LocalDate、LocalDateTime序列化
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));

        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN);
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));

        objectMapper.registerModule(javaTimeModule);
        // 时间格式化
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setDateFormat(new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN));

        objectMapper.registerModule(new JavaTimeModule());


        return objectMapper;
    }

    @Bean
    public ObjectMapper objectMapper() {
        return createObjectMapper();
    }
    
    @Bean
    public LogService logService() {
        return new LogService();
    }
    
    @Bean
    public RequestLogRecord requestLogRecord(LogService logService) {
        return new RequestLogRecord() {
            
            @Override
            public void record(LogAPIMessage message) {
                try {
                    LoginUserVO currentUser = LoginInfoContext.getLoginInfo().getUser();
                    // 设置操作用户id
                    message.setOperateUserId(currentUser.getId());
                    // 设置操作用户名称
                    message.setOperateRealname(currentUser.getRealname());
                } catch (Exception e) {
                }
                logService.sendLogMessage(message);
            }
        };
    }
}
